package com.llg.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llg.mapper.InputInterMapper;
import com.llg.model.code.CommonCode;
import com.llg.model.code.PageCode;
import com.llg.model.response.DataResult;
import com.llg.model.response.PageResult;
import com.llg.model.response.ResponseResult;
import com.llg.pojo.InputInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class InputInterService {

    @Autowired
    private InputInterMapper mapper;

    //查所有
    public PageResult<InputInter> findAll(){
        List<InputInter> list = mapper.selectAll();
        return new PageResult<>(CommonCode.SUCCESS, list.size(), 1, list);
    }

    //通过id查
    public DataResult<InputInter> findById(int id){
        return new DataResult<>(CommonCode.SUCCESS, mapper.selectByPrimaryKey(id));
    }


    //搜索分页查
    public PageResult<InputInter> findList(int currentPage,int pageSize,InputInter inputInter){
        //参数判断
        if(currentPage <= 0) return new PageResult<>(PageCode.PAGE_ERR1, 0, 0, null);
        if(pageSize <= 0) return new PageResult<>(PageCode.SIZE_ERR, 0, 0, null);
        PageHelper.startPage(currentPage, pageSize);
        Example example = new Example(InputInter.class);
        Example.Criteria criteria = example.createCriteria();
        if (inputInter != null & !inputInter.getInter().equals("")) {
            criteria.andLike("inter", "%" + inputInter.getInter() + "%");
        }
        List<InputInter> list = mapper.selectByExample(example);
        PageInfo<InputInter> page = new PageInfo<>(list);
        return new PageResult<>(CommonCode.SUCCESS,(int)page.getTotal(),page.getPages(),page.getList());

    }

    //增
    public DataResult<InputInter> add(InputInter inputInter){
        mapper.insertSelective(inputInter);
        return new DataResult<>(CommonCode.SUCCESS, inputInter);
    }

    //删
    public ResponseResult delete(int id){
        mapper.deleteByPrimaryKey(id);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //改
    public ResponseResult update(InputInter inputInter){
        mapper.updateByPrimaryKeySelective(inputInter);
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
